package dbsucxentW.statisticsW;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * Created on 28 May 2008
 * @author: Klarinda
 * Objective: Class to get the statistics of the data from the database
 * 			  (PathCount, TotalPathCount, ValueSelectivity)
 * Please refer to Sandeep's Prefetching paper for definition of each term 
 */

public class XMLDataStatisticsGetter {

	private Connection _dbConnection;
	boolean _isSucxent = true;
	
	public XMLDataStatisticsGetter(Connection conn, boolean isSucxent) {
		_dbConnection = conn;
		_isSucxent = isSucxent;
	}
	
	public XMLDataStatistics getXMLDataStatistics() {
		XMLDataStatistics stats = new XMLDataStatistics();
	
		String sql1, sql2;
		
		if(_isSucxent) {
			sql1 = 	"select PV.PathId, pathexp, CP, VP \n"+
					"from pathcount_valueselectivity PV, path P \n"+
					"where pv.pathid = p.pathid \n";
		}
		else {	//edge
			sql1 = 	"select PV.Path_id, path, CP, VP \n"+
					"from pathcount_valueselectivity PV, path P \n"+
					"where pv.path_id = p.path_id \n";
		}
		sql2 =   "select * from totalpathcount \n";
		
		try {
			Statement statement = _dbConnection.createStatement();
			System.out.println("getXMLDataStatistics: \n"+sql1);
			ResultSet results = statement.executeQuery (sql1);
			while (results.next()) {
				//stats.add(int PathId, String Path, int PathCount, int ValueSelectivity) 
				stats.add(results.getInt(1),
							results.getString(2).trim(),	//need to trim the result
							results.getInt(3),
							results.getInt(4)
				);
			}

			System.out.println("getXMLDataStatistics: \n"+sql2);
			results = statement.executeQuery (sql2);
			while (results.next()) {
				stats.setTotalPathCount(results.getInt(1));
			}			
			
			System.out.println("getXMLDataStatistics, statistics:\n"+stats.toString());
			results.close();
			statement.close();

			
		} catch (Exception ex){
    		System.out.println(ex.getMessage());
    	}
		return stats;
	}
}
